﻿using Student.BLL;
using Student.ViewModel.Enum;
using Student.ViewModel;
using Student.Common;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace Student.Api.Attributes
{
    internal class ValidTokenAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var _userTokenBO = new UserTokenBO();

            var token = context.ActionArguments["token"]?.ToString();
            if (string.IsNullOrEmpty(token))
            {
                context.Result = new JsonResult(new Result(ResultCode.TokenMissing));
            }
            else
            {
                bool result = _userTokenBO.ValidToken(token);
                if (!result)
                {
                    context.Result = new JsonResult(new Result(ResultCode.TokenError));
                }
            }

            base.OnActionExecuting(context);
        }
    }
}
